//
//  HCStatusPictureView.swift
//  Hweibome
//
//  Created by apple on 15/12/11.
//  Copyright © 2015年 hechuan. All rights reserved.
//

import UIKit
import SnapKit

private let HCStatusPictureViewItemId = "HCStatusPictureViewItemId"

private let itemMargin: CGFloat = 5
private let itemWH = (SCREENW - itemMargin*2 - HCStatusCellMargin*2)/3


class HCStatusPictureView: UICollectionView {
    var sizeConstraint:Constraint?
    
    var pic_urls:[HCStatusPictureInfo]?{
        didSet{
            
            
            countLabel.text = "\(pic_urls?.count ?? 0)"
            
            let size = calcSize(pic_urls?.count ?? 0)
            
            self.snp_updateConstraints { (make) -> Void in
                self.sizeConstraint = make.size.equalTo(size).constraint
            }
            
            reloadData()
        }
        
    }

    private func calcSize(count: Int) ->CGSize {
        let itemMargin: CGFloat = 5
        let itemWH = (SCREENW - itemMargin*2 - HCStatusCellMargin*2)/3
        
        let col = count == 4 ? 2 : (count >= 3 ? 3 :count)
        let row = count == 4 ? 2 : ((count-1)/3 + 1 )
        
        let width = CGFloat(col)*itemWH + CGFloat(col-1)*itemMargin
        let height = CGFloat(row)*itemWH + CGFloat(row-1)*itemMargin
        
        
        if col == 2 {
            print(CGSize(width: width, height: height))
        }
        
        return CGSize(width: width, height: height)
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
       
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
        
        registerClass(HCStatusPictureViewCell.self,  forCellWithReuseIdentifier: HCStatusPictureViewItemId)
        dataSource = self
        delegate = self
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
       // 此处删除了计算图片数量的label的显示
       // addSubview(countLabel)
        

      //  countLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(self)
//        }
    }
    
    
    private lazy var countLabel: UILabel = UILabel(textColor: UIColor.blackColor(), fontSize: 40)
}

//cell 的数据源方法
extension HCStatusPictureView :UICollectionViewDataSource , UICollectionViewDelegate
{
    
 
    // 返回有多少个cell
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    
    // 返回cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HCStatusPictureViewItemId, forIndexPath: indexPath) as! HCStatusPictureViewCell
        cell.pictureInfo = pic_urls![indexPath.row]
        return cell
    }

 //    代理方法
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        printLog(indexPath.row)
    }
    
}


class HCStatusPictureViewCell: UICollectionViewCell {
    
    var pictureInfo: HCStatusPictureInfo? {
        didSet{
            if let urlString = pictureInfo?.thumbnail_pic{
                
                gifImageView.hidden = !urlString.hasSuffix(".gif")
                imgView.sd_setImageWithURL(NSURL(string: urlString), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI()
    {
      backgroundColor = RandomColor()
        contentView.addSubview(imgView)
        contentView.addSubview(gifImageView)
        
        imgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imgView)
            make.bottom.equalTo(imgView)
        }
        
    }

    
    private lazy var imgView :UIImageView = {
        let imageView = UIImageView()
        
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    private lazy var gifImageView:UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
}
